package core

import grails.transaction.Transactional

@Transactional
class CommonService {

    def serviceMethod() {

    }
	
	def fillCountries() {
		if (Country.first()) 
			return
		 
		def countryXml = new XmlSlurper().parse("http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_xml-temp.htm")
		countryXml.children().each {
			def countryEntry = it.children()
			def name = countryEntry[0].toString();
			def code = countryEntry[1].toString();
			if( (new Country(name: name, code:code)).save())
				log.error("Country : $name is added")
		}
	}
	
	def fillDocumentType() {
		if (DocumentType.first())
			return
		
		['ID','Passport', 'Birth Certificate'].each {
			new DocumentType(name:it).save()
		}
	}
	
	def fillPeople() {
		if (Person.first())
			return
		['Najeh','Ahmad', 'Najeeb','Osama', 'Hashem', 'Raed'].sort{new Random().nextInt()}.each { first ->
			['Ali','Khraiwish','Nazzal','Mohammad','Yasin','Hindaileh'].sort{new Random().nextInt()}.each { second ->
				def yy = new Random().nextInt(88)+10
				def mm = new Random().nextInt(11)+1
				def dd = new Random().nextInt(27)+1
				def p = new Person(firstName:first, lastName:second,birthDate:(new Date("19${yy}/${mm}/${dd}")))
				def maxLegalIndentifiers = new Random().nextInt(2)+1
				p.save()
				maxLegalIndentifiers.times{
					def iden = new LegalIdentifier(idValue:p.birthDate.format('yyMMdd'),
						issuingCountry:Country.list(max:1,offset:new Random().nextInt(240))[0],
						documentType: DocumentType.list(max:1,offset:new Random().nextInt(3))[0],
						validFrom:p.birthDate,
						person:p)
					iden.save()
				}
				
			}
		}
	}
}
